package com.jysz.certificate.web.action.system;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jysz.certificate.action.AbstractBaseAction;
import com.jysz.certificate.persistence.enums.RegionEnum;
import com.jysz.certificate.persistence.model.system.Region;
import com.jysz.certificate.persistence.model.system.RegionDTO;
import com.jysz.certificate.service.system.RegionService;

@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespace("/system/region")
public class RegionAction  extends AbstractBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Region entity;
	private RegionDTO search;
	private List<Region> list;
	private Long pid;//父节点ID
//	private String pname;//父节点名称
	
	@Resource
	private RegionService regionService;

	@Action(INDEX)//查询页面**驻店所有
	public String index() {
		
		this.setPage(regionService.findPage(search,getCurrentPage(), getPageSize()));
		
		return INDEX;
	}
	
	@Action("add")//新增页面**驻店
	public String add() {
		entity=new Region();
		if(pid!=null){//pid不为空,增加子项(市)
			entity.setType(RegionEnum.CITY);
		}else{//pid为空,增加省
			pid=1L;//确保 ‘中国’的id为1
			entity.setType(RegionEnum.PROVINCE);
		}
		Region parent=regionService.find(pid);
		entity.setParent(parent);
		setRedirectUrl("index.do?currentPage="+getCurrentPage(),"search",search);
		return "add";
	}
	
	@Action("create")//新增页面**驻店
	public String create() {
		
		Region parent=regionService.find(pid);
		entity.setParent(parent);
		entity.setType(RegionEnum.next(parent.getType()));
		regionService.save(entity);
		setSuccessPopMessage("新增成功");
		return SHOW_MSG_PAGE;
	}
	
	@Action("edit")//新增页面**驻店
	public String edit() {
		entity=regionService.find(entity.getId());
		setRedirectUrl("index.do?currentPage="+getCurrentPage(),"search",search);
		return "edit";
	}
	
	@Action("update")//新增页面**驻店
	public String update() {
		Region region=regionService.find(entity.getId());
		region.setName(entity.getName());
		regionService.save(region);
		setSuccessPopMessage("更新成功");
		return SHOW_MSG_PAGE;
	}
	
	@Action("destory")
	public String destory(){
		
		regionService.destory(entity.getId());
		setRedirectUrl("index.do?currentPage=" + getCurrentPage(), "search", search);
		setSuccessPopMessage("删除数据成功");
		return SHOW_MSG_PAGE;
	}
	
	@Action("view")//查询页面**驻店-id
	public String view() {
//		entity=shopService.minuteShop(search.getId());
		
		return "view";
	}
	
	public Region getEntity() {
		return entity;
	}

	public void setEntity(Region entity) {
		this.entity = entity;
	}

	public List<Region> getList() {
		return list;
	}

	public void setList(List<Region> list) {
		this.list = list;
	}

	public RegionDTO getSearch() {
		return search;
	}

	public void setSearch(RegionDTO search) {
		this.search = search;
	}

	public Long getPid() {
		return pid;
	}

	public void setPid(Long pid) {
		this.pid = pid;
	}
	
}
